/**
 * 
 */
package com.ekingstar.framework.security;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.collections.iterators.IteratorEnumeration;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 *
 */
public class FunctionParamRequestWrapper extends HttpServletRequestWrapper {

	public FunctionParamRequestWrapper(HttpServletRequest request) {
		super(request);
		// TODO Auto-generated constructor stub
	}
	private Map funParams ;
	private ValueStack stack;
	public FunctionParamRequestWrapper(HttpServletRequest request,Map funParams,ValueStack stack) {
		super(request);
		// TODO Auto-generated constructor stub
		this.stack = stack;
		this.funParams = funParams;
	}
	public String getParameter(String name){
		String[] vals = this.getParameterValues(name);
		if (vals != null )return vals[0];
		return null;
	}

    public Enumeration getParameterNames(){
    	Map params = this.getParameterMap();
    	if (params == null ) return null;
    	Iterator it = params.keySet().iterator();
    	Enumeration en = new IteratorEnumeration(it);
    	return en;
    }

    public String[] getParameterValues(String name){
    	return (String[])getParameterMap().get(name);
    	
    }
    public Map getParameterMap(){
    	if (funParams == null)  {
    		return super.getParameterMap();
    	}else {
    		Map params = super.getParameterMap();
    		Map retParams = new HashMap();
    		retParams.putAll(params);
    		if (stack != null){
    			Iterator it = funParams.entrySet().iterator();
    			while(it.hasNext()){
    				Map.Entry e= (Map.Entry)it.next();
    				Object val = e.getValue();
    				Object key = e.getKey();
    				val = StatckHashMapUtil.get(val, stack);
    				retParams.put(key, val);
    			}
    		}else{
    			retParams.putAll(funParams);
    		}
    		;
    		return retParams;
    	}
    }
	
}
